﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using Infrastructure;
using SverigesRadio.SverigesRadioApp.Model;

namespace SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.ViewModels
{
    public class ProgramsViewModel : ViewModelBase
    {
        private SverigesRadioServiceCaller<CategoryPrograms> programService;
        public const string CategoryIdQueryString = "CategoryId";
        public const string CategoryNameString = "CategoryName";

        public ProgramsViewModel(MainViewModel parentViewModel)
        {
            ParentViewModel = parentViewModel;

            programService = new SverigesRadioServiceCaller<CategoryPrograms>();
            programService.OnServiceCallReceived += ProgramService_OnServiceCallReceived;

            ParentViewModel.NavigationService.Navigated += NavigationService_Navigated;
        }


        private IList<Group<Program>> programs;
        public IList<Group<Program>> Programs
        {
            get { return programs; }
            set
            {
                programs = value;
                RaisePropertyChanged("Programs");
            }
        }

        private string categoryTitleText;
        public string CategoryTitleText
        {
            get { return categoryTitleText; }
            set
            {
                categoryTitleText = value;
                RaisePropertyChanged("CategoryTitleText");
            }
        }

        private Program selectedProgram;

        public Program SelectedProgram
        {
            get { return selectedProgram; }
            set
            {
                if (value == null)
                    return;

                selectedProgram = value;

                ParentViewModel.NavigationService.NavigateTo(ViewType.ProgramInformation, 
                    ProgramInformationViewModel.ProgramIdQueryString + "=" + value.Id, 
                    CategoryIdQueryString + "=" + CategoryTitleText,
                    ProgramInformationViewModel.ProgramTitleQueryString + "=" + value.Title);
            }
        }

        void ProgramService_OnServiceCallReceived(object sender, SverigesRadioServiceCallerEventArgs<CategoryPrograms> e)
        {
            ParentViewModel.IsBusy = false;

            if(CategoryTitleText == ProgramCategoriesControlViewModel.TitleText_ArchivedPrograms)
            {
                Programs = (from program in e.ServiceResult.Programs
                            where program.Archived
                            orderby program.FirstCapital
                            group program by program.FirstCapital
                                into fc
                                orderby fc.Key
                                select new Group<Program>(fc.Key.ToString(), fc)).ToList();
            }
            else
            {
                Programs = (from program in e.ServiceResult.Programs
                            where program.Archived == false
                            orderby program.FirstCapital
                            group program by program.FirstCapital
                                into fc
                                orderby fc.Key
                                select new Group<Program>(fc.Key.ToString(), fc)).ToList();
            }


        }

        public MainViewModel ParentViewModel { get; private set; }


        void NavigationService_Navigated(object sender, NavigatedEventHandler e)
        {
            if (e.ViewType != ViewType.Programs)
                return;

            ParentViewModel.IsBusy = true;

            CategoryTitleText = e.QueryStrings[CategoryNameString];

            if (e.QueryStrings.ContainsKey(CategoryIdQueryString))
                programService.CallAsync(SverigesRadioServices.CategoryPrograms, 
                    CategoryIdQueryString + "=" + e.QueryStrings[CategoryIdQueryString]);
            else
            {
                programService.CallAsync(SverigesRadioServices.CategoryPrograms);                                                                                                                                                                                                                                          
            }

        }


    }
}
